package com.example.springboot.controller;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Attendance;
import com.example.springboot.entity.Department;
import com.example.springboot.entity.Leaves;
import com.example.springboot.entity.User;
import com.example.springboot.service.IAttendanceService;
import com.example.springboot.service.IDepartmentService;
import com.example.springboot.service.ILeavesService;
import com.example.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 可视化
 */
@RestController
@RequestMapping("/echarts")
public class EchartsController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IAttendanceService attendanceService;

    @Autowired
    private ILeavesService leavesService;

    /**
     * 柱状图
     * @return
     */
    @GetMapping("/main1")
    public Result main1(){
        List<Department> departments = departmentService.list();
        List<Dict> barList = new ArrayList<>();
        for (Department department : departments) {
            String flag = department.getFlag();
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("department", flag);
            List<User> usersList = userService.list(queryWrapper);
            int userCount = usersList.size();
            Dict dict = new Dict();
            dict.set("userCount", userCount).set("departmentName", department.getDepartmentName());
            barList.add(dict);
        }
        return Result.success(barList);
    }

    /**
     * 饼图1
     * @return
     */
    @GetMapping("/main2")
    public Result main2(){
        Integer presentUsers = 0;
        Integer absentUsers = 0;
        Integer onLeaveUsers = 0;
        List<Attendance> attendances = attendanceService.list();
        List<Leaves> leaves = leavesService.list();
        for (Attendance attendance : attendances) {
            if ("否".equals(attendance.getAbsentType())){
                presentUsers++;
            }
            if ("是".equals(attendance.getAbsentType())){
                absentUsers++;
            }
        }
        onLeaveUsers = leaves.size();
        Dict attendanceData = new Dict();
        attendanceData.set("缺勤人次", absentUsers)
                .set("请假人次", onLeaveUsers)
                .set("不缺勤人次", presentUsers);

        return Result.success(attendanceData);
    }

    /**
     * 饼图2
     * @return
     */
    @GetMapping("/main3")
    public Result main3(){
        List<User> users = userService.list();
        int age20_29 = 0;
        int age30_39 = 0;
        int age40_49 = 0;
        int age50_59 = 0;
        int age60Above = 0;

        for (User user : users) {
            if (user.getAge() != null) {
                int age = user.getAge();

                if (age >= 20 && age <= 29) {
                    age20_29++;
                } else if (age >= 30 && age <= 39) {
                    age30_39++;
                } else if (age >= 40 && age <= 49) {
                    age40_49++;
                } else if (age >= 50 && age <= 59) {
                    age50_59++;
                } else if (age >= 60) {
                    age60Above++;
                }
            }
        }

        Dict ageGroupData = new Dict();
        ageGroupData.set("20-29", age20_29)
                .set("30-39", age30_39)
                .set("40-49", age40_49)
                .set("50-59", age50_59)
                .set("60及以上", age60Above);

        return Result.success(ageGroupData);
    }

    /**
     * 散点图
     * @return
     */
    @GetMapping("/main4")
    public Result main4(){
        List<User> users = userService.list();
        Map<String, List<Dict>> departmentDataMap = new HashMap<>();

        for (User user : users) {
            String department = user.getDepartment();
            String departmentName = departmentService.getName(department);
            Integer age = user.getAge();
            Double salary = user.getSalary();

            if (departmentName != null && age != null && salary != null) {
                Dict dataPoint = new Dict();
                dataPoint.set("age", age);
                dataPoint.set("salary", salary);

                departmentDataMap.computeIfAbsent(departmentName, k -> new ArrayList<>()).add(dataPoint);
            }
        }

        return Result.success(departmentDataMap);
    }

    //日期转换工具
    private static String TimeToString(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }


}
